package adapter;

import java.util.Locale;
import java.util.ResourceBundle;

import player.Player;
import desktop_codebehind.Car;
import desktop_resources.GUI;
import desktop_fields.*;

public class Adapter {

	private ResourceBundle msg;
	private Locale currentLocale;
	private desktop_fields.Field[] guifelter;


	/**
	 * Definerer spillets sprog så det matcher systemets sprog.
	 * Hvis systemets sprog ikke er defineret i spillet bruges default (da_DK)
	 */
	public Adapter(){
		currentLocale = Locale.getDefault();
		msg = ResourceBundle.getBundle("MessagesBundle", currentLocale);
	}
	
	public Adapter(desktop_fields.Field[] guifelt){
		currentLocale = Locale.getDefault();
		msg = ResourceBundle.getBundle("MessagesBundle", currentLocale);
		this.guifelter = guifelt;
	}
	

	public ResourceBundle getBundle(){
		return msg;
	}

	// **************************
	// Sådan bruges resourceBundle
	//**************************
	public void testMetode(){
		System.out.println(msg.getString("Test")); // Udskriver tekststrengen der er defineret i MessagesBundle.properties for Keyword "Test"
	}

	public String showTypeName(){
		String i = GUI.getUserString(msg.getString("Enter_Player_Name"));
		return i;
	}

	public String player(){
		return msg.getString("Player");
	}

	public int chooseCar(String playerName){
		int i = 4;
		String type = GUI.getUserButtonPressed(playerName + " : " + msg.getString("Choose_Car"), msg.getString("RaceCar"), msg.getString("Ufo"), msg.getString("Tractor"), msg.getString("Car"));
		if (type == msg.getString("RaceCar")){
			i = 1;
		}
		if (type == msg.getString("Ufo")){
			i = 2;
		}
		if (type == msg.getString("Tractor")){
			i = 3;
		}
		if (type == msg.getString("Car")){
			i = 4;
		}
		return i;
	}

	public void addPlayer(String name, int balance, Car car){
		GUI.addPlayer(name, balance, car);
	}

	public void setCar(int fieldNumber, String name) {
		GUI.setCar(fieldNumber, name);
	}

	public void removeCar(int fieldNumber, String name) {
		GUI.removeCar(fieldNumber, name);
	}

	public void setOwner(int fieldNumber, String name) {
		GUI.setOwner(fieldNumber+1, name);
	}

	public void removeOwner(int fieldNumber) {
		GUI.removeOwner(fieldNumber+1);
	}

	public void setDice(int faceValue1, int rotation1, int x1, int y1,
			int faceValue2, int rotation2, int x2, int y2) {
		GUI.setDice(faceValue1, rotation1, x1, y1, faceValue2, rotation2, x2, y2);
	}

	public void setBalance(String name, int newBalance) {
		GUI.setBalance(name, newBalance);
	}

	public void setSubText(int fieldNumber, String subText) {
		GUI.setSubText(fieldNumber+1, subText);
	}

	public void showMessage(String message){
		GUI.showMessage(message);
	}

	public void showFieldDesc(Player player){
		//GUI.showMessage(field[player.getPosition()-1].getDesc());
	}

	public void rollDice(String name){
		GUI.showMessage(name+ " : " + msg.getString("Roll_Dice"));
	}

	public void winner(String name){
		GUI.showMessage(msg.getString("Grats") + name + ". " + msg.getString("You_Won"));
	}

	public void draw(String name){
		GUI.showMessage(msg.getString("Grats") + name + msg.getString("You_Won"));
	}

	public boolean buyField(String playerName){
		String i = GUI.getUserSelection(playerName + " : " + msg.getString("Buy_Field"), msg.getString("Yes"), msg.getString("No"));
		if (i == msg.getString("Yes")){
			return true; }
		else {
			return false;}
	}

	public void cannotAfford(){
		GUI.showMessage(msg.getString("Cannot_Afford"));
	}

	public void yourField(){
		GUI.showMessage(msg.getString("Your_Field"));
	}

	public boolean taxAmount(int cnsq, int value, String playerName){
		String n = String.valueOf(cnsq);
		String i = GUI.getUserSelection(playerName + " : " + msg.getString("Tax"), n, msg.getString("10PCT") + value);
		if (i == n){
			return true;
		}
		else {
			return false;
		}
	}

	public void error(){
		System.out.println(msg.getString("Error"));
		GUI.showMessage(msg.getString("Error"));
		GUI.close();
		System.exit(0);
	}
	
	public void setHouses(int field, int houseCount){
		GUI.setHouses(field+1, houseCount);
	}
	
	public void setHotels(int field, boolean hotel){
		GUI.setHotel(field+1, hotel);
	}

	/**
	 * Viser menuknapper i GUI
	 * @param createButtons Array af Strings med knapper der skal vises
	 * @return Hvilken knap der er trykket på i GUIen.
	 */
	public int menu(String[] createButtons, String playerName) {
		int output = 0;
		
		String n = GUI.getUserButtonPressed(playerName + " : " + msg.getString("Main_Menu"), createButtons);

		if (n == msg.getString("Buy_House")){
			output = 0;
		}
		else if (n == msg.getString("Sell_House")){
			output = 1;
		}
		else if (n == msg.getString("Pawn_Ground")){
			output = 2;
		}
		else if (n == msg.getString("Unpawn_Ground")){
			output = 3;
		}
		else if (n == msg.getString("End_Turn")){
			output = 4;
		}
		else if (n == msg.getString("Sell_Ground")){
			output = 5;
		}
		return output;
	}
	
	public int jailMenu(String playerName) {
		int output = 0;
		
		String n = GUI.getUserButtonPressed(playerName + " : " + msg.getString("Jail_Menu"), msg.getString("Jail_Roll"),msg.getString("Jail_Pay"));

		if (n == msg.getString("Jail_Roll")){
			output = 0;
		}
		else if (n == msg.getString("Jail_Pay")){
			output = 1;
		}
		return output;
	}

	public String menuDropDown(String[] choice, String playerName) {		
		String n = GUI.getUserSelection(playerName + " : " + msg.getString("Sub_Menu"), choice);
		return n;
	}

	public void pawn(int field) {
		GUI.setSubText(field+1, msg.getString("Pawned"));
	}

	public void unPawn(int field, Integer rent) {
		GUI.setSubText(field+1, rent.toString());
		
	}

	public boolean getUserTaxChoice(String fixed, String playerName) {
		String choice = GUI.getUserSelection(playerName + " : " + msg.getString("Tax_Choice"), fixed, msg.getString("Tax_10pct"));
		if (choice == fixed){
			return false;
		}
		else{
			return true;
		}
	}

	public void setSubTextBrewery(int n, String rent) {
		GUI.setSubText(n+1, rent + msg.getString("Brewery_Sub"));
	}

	/**
	 * Opdaterer subtekst for felter af typen Property på GUI
	 * @param n feltnummeret
	 * @param rent Prisen for at lande på feltet
	 */
	public void setSubTextProperty(int n, String rent) {
		GUI.setSubText(n+1, msg.getString("Property_Rent") + rent);
	}
	
	/**
	 * Opdaterer rundenummeret på Start-feltet
	 * @param n Antal runder tilbage
	 */
	public void updRounds(int n){
		GUI.setTitleText(1, Integer.toString(n));
	}
	
	/**
	 * Viser et felts beskrivelse på midten af GUI når der landes på det
	 * @param feltnr Feltnummeret der landes på (0-39)
	 */
	public void setCenter(int feltnr){
		guifelter[feltnr].displayOnCenter();
	}

	public int getInt(String fieldTitle, String playerName) {
		int output = GUI.getUserInteger(playerName + " : " + msg.getString("Set_Price") + fieldTitle);
		return output;
	}



}

